package cn.cdeden.resource.utils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.zip.*;

import org.apache.commons.compress.archivers.zip.ZipFile;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import java.util.Enumeration;

public class SimpleZipTestUnzip {
    public static void unzipFile(String zipFilePath, String destDir) throws IOException {
        // 创建输入流
        try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFilePath), StandardCharsets.UTF_8)) {
            // 解压缩每个条目
            ZipEntry entry;
            while ((entry = zis.getNextEntry()) != null) {
                // 打印文件名，查看解压的文件
                System.out.println("解压文件: " + entry.getName());

                File destFile = new File(destDir, entry.getName());

                // 如果目标是目录，跳过
                if (entry.isDirectory()) {
                    destFile.mkdirs();
                } else {
                    // 确保目标文件的父目录存在
                    File parentDir = destFile.getParentFile();
                    if (!parentDir.exists()) {
                        parentDir.mkdirs();
                    }

                    // 写文件到目标目录
                    try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile))) {
                        byte[] buffer = new byte[1024];
                        int len;
                        while ((len = zis.read(buffer)) != -1) {
                            bos.write(buffer, 0, len);
                        }
                    }
                }
                zis.closeEntry();
            }
        }
    }

    public static void unzip(String zipFilePath, String destDir) throws IOException {
        try (ZipFile zipFile = new ZipFile(zipFilePath)) {
            Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
            while (entries.hasMoreElements()) {
                ZipArchiveEntry entry = entries.nextElement();
                File destFile = new File(destDir, entry.getName());

                // 创建目录（如果是目录条目）
                if (entry.isDirectory()) {
                    destFile.mkdirs();
                    continue;
                }

                // 确保父目录存在
                File parentDir = destFile.getParentFile();
                if (!parentDir.exists()) {
                    parentDir.mkdirs();
                }

                // 写入文件
                try (InputStream is = zipFile.getInputStream(entry);
                     BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile))) {

                    byte[] buffer = new byte[1024];
                    int len;
                    while ((len = is.read(buffer)) != -1) {
                        bos.write(buffer, 0, len);
                    }
                }
            }
        }
        System.out.println("解压完成");
    }



    private static void extractFile(ZipInputStream zis, String filePath) throws IOException {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
        byte[] bytesIn = new byte[4096];
        int read = 0;
        while ((read = zis.read(bytesIn)) != -1) {
            bos.write(bytesIn, 0, read);
        }
        bos.close();
    }

    public static void main(String[] args) {
        try {
            unzip("D:\\test\\word文件10k到100m各一个.zip", "D:\\test\\word文件10k到100m各一个\\");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
